<?php
class service extends core {
	
	public function __construct() {
		parent::__construct();
	}
	
	public function province() {
		$where = '';
		$rs = SQL::share('service_province sp')
			->left('province p', 'sp.province_id=p.province_id')
			->sort('sp.id ASC')->isezr()->find('sp.*, p.name');
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',SQL::share()->nav);
		$this->display();
	}
	
	public function province_edit() {
		$id = $this->request->get('id', 0);
		if (IS_POST) {
			$id = $this->request->post('id', 0);
			$province_id = $this->request->post('province_id', 0);
			$status = $this->request->post('status', 0);
			$sort = $this->request->post('sort', 0);
			$data = array('province_id'=>$province_id, 'status'=>$status, 'sort'=>$sort);
			if ($id>0) {
				SQL::share('service_province')->where($id)->update($data);
			} else {
				SQL::share('service_province')->insert($data);
			}
			location("?app=service&act=province");
		} else if ($id>0) {
			$row = SQL::share('service_province')->where($id)->find();
		} else {
			$row = t('service_province');
		}
		$this->smarty->assign('row',$row);
		$this->display();
	}
	
	public function province_delete() {
		$id = $this->request->get('id', 0);
		SQL::share('service_province')->delete($id);
		location("?app=service&act=province");
	}
	
	public function city() {
		$where = '';
		$province_id = $this->request->get('province_id', 0);
		if ($province_id<=0) error('缺少参数');
		if ($province_id) {
			$where .= " AND sp.province_id='{$province_id}'";
		}
		$rs = SQL::share('service_city sc')
			->left('city c', 'sc.city_id=c.city_id')
			->left('service_province sp', 'sp.id=sc.parent_id')
			->left('province p', 'sp.province_id=p.province_id')
			->where("1 {$where}")->sort('sc.id ASC')->isezr()->find('sc.*, c.name, p.name as province_name');
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',SQL::share()->nav);
		$this->display();
	}
	
	public function city_edit() {
		$id = $this->request->get('id', 0);
		if (IS_POST) {
			$id = $this->request->post('id', 0);
			$province_id = $this->request->post('province_id', 0);
			$city_id = $this->request->post('city_id', 0);
			$end_day = $this->request->post('end_day', 20);
			$min_months = $this->request->post('min_months', 1);
			$service_money = $this->request->post('service_money', 0, 'float');
			$gjj_service_money = $this->request->post('gjj_service_money', 0, 'float');
			//手续费折扣
			$service_discount_month = $this->request->post('service_discount_month', 0);
			$gjj_service_discount_month = $this->request->post('gjj_service_discount_month', 0);
			$service_discount_month_money = $this->request->post('service_discount_month_money', 0, 'float');
			$gjj_service_discount_month_money = $this->request->post('gjj_service_discount_month_money', 0, 'float');
			$money = $this->request->post('money', 0, 'float');
			$max_money = $this->request->post('max_money', 0, 'float');
			$person = $this->request->post('person', 0);
			$firm = $this->request->post('firm', 0);
			$status = $this->request->post('status', 0);
			$sort = $this->request->post('sort', 0);
			if ($end_day<=0) error('截止日期错误');
			if ($min_months<=0) $min_months = 1;
			$parent_id = SQL::share('service_province')->where("province_id='{$province_id}'")->val('id');
			$data = compact('parent_id', 'city_id', 'end_day', 'min_months', 'service_money', 'gjj_service_money',
				'money', 'max_money', 'person', 'firm', 'status', 'sort', 
				'service_discount_month', 'gjj_service_discount_month', 'service_discount_month_money', 'gjj_service_discount_month_money');
			if ($id>0) {
				SQL::share('service_city')->where($id)->update($data);
			} else {
				SQL::share('service_city')->insert($data);
			}
			location("?app=service&act=city&province_id={$province_id}");
		} else if ($id>0) {
			$row = SQL::share('service_city')->where($id)->find();
		} else {
			$row = t('service_city');
		}
		$this->smarty->assign('row',$row);
		$this->display();
	}
	
	public function city_delete() {
		$id = $this->request->get('id', 0);
		$province_id = $this->request->get('province_id', 0);
		SQL::share('service_city')->delete($id);
		$rs = SQL::share('service_casy')->where("service_city_id='{$id}'")->find('id');
		if ($rs) {
			foreach ($rs as $k=>$g) {
				SQL::share('service_casy')->delete($g->id);
				SQL::share('service_casy_detail')->delete("casy_id='{$g->id}'");
			}
		}
		location("?app=service&act=city&province_id={$province_id}");
	}
	
	public function casy() {
		$province_id = $this->request->get('province_id', 0);
		$service_city_id = $this->request->get('service_city_id', 0);
		if ($province_id<=0 || $service_city_id<=0) error('缺少参数');
		$rs = SQL::share('service_casy')->where("service_city_id='{$service_city_id}'")->isezr()->find();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',SQL::share()->page);
		$city_name = SQL::share('service_city sc')
			->left('city c', 'c.city_id=sc.city_id')
			->where("sc.id='{$service_city_id}'")->val('c.name');
		$this->smarty->assign('city_name',$city_name);
		$this->display();
	}
	
	public function casy_edit() {
		$id = $this->request->get('id', 0);
		$province_id = $this->request->get('province_id', 0);
		$service_city_id = $this->request->get('service_city_id', 0);
		if (IS_POST) {
			$id = $this->request->post('id', 0);
			$province_id = $this->request->post('province_id', 0);
			$service_city_id = $this->request->post('service_city_id', 0);
			$name = $this->request->post('name');
			$money = $this->request->post('money', 0);
			if ($province_id<=0 || $service_city_id<=0 || !$name) error('缺少参数');
			$data = array('service_city_id'=>$service_city_id, 'name'=>$name, 'money'=>$money);
			if ($id>0) {
				SQL::share('service_casy')->where($id)->update($data);
			} else {
				$id = SQL::share('service_casy')->insert($data);
			}
			location("?app=service&act=casy&province_id={$province_id}&service_city_id={$service_city_id}");
		} else if ($id>0) {
			$province_id = $this->request->get('province_id', 0);
			$service_city_id = $this->request->get('service_city_id', 0);
			if ($province_id<=0 || $service_city_id<=0) error('缺少参数');
			$row = SQL::share('service_casy')->where($id)->find();
		} else {
			$row = t('service_casy');
		}
		$this->smarty->assign('row',$row);
		$city_name = SQL::share('service_city sc')
			->left('city c', 'c.city_id=sc.city_id')
			->where("sc.id='{$service_city_id}'")->val('c.name');
		$this->smarty->assign('city_name',$city_name);
		$this->display();
	}
	
	public function casy_delete() {
		$id = $this->request->get('id', 0);
		$province_id = $this->request->get('province_id', 0);
		$service_city_id = $this->request->get('service_city_id', 0);
		if ($province_id<=0 || $service_city_id<=0) error('缺少参数');
		SQL::share('service_casy')->delete($id);
		SQL::share('service_casy_detail')->delete("casy_id='{$id}'");
		location("?app=service&act=casy&province_id={$province_id}&service_city_id={$service_city_id}");
	}
	
	public function casy_detail() {
		if (IS_POST) {
			$casy_id = $this->request->post('casy_id', 0);
			$province_id = $this->request->post('province_id', 0);
			$service_city_id = $this->request->post('service_city_id', 0);
			$name = $this->request->post('name', '', 'origin');
			$money = $this->request->post('money', '', 'origin');
			$firm = $this->request->post('firm', '', 'origin');
			$person = $this->request->post('person', '', 'origin');
			if ($province_id<=0 || $service_city_id<=0 || !$casy_id) error('缺少参数');
			SQL::share('service_casy_detail')->delete("casy_id='{$casy_id}'");
			$data = array('casy_id'=>$casy_id, 'name'=>$name, 'money'=>$money, 'firm'=>$firm, 'person'=>$person);
			SQL::share('service_casy_detail')->insert($data, 'name');
			location("?app=service&act=casy&province_id={$province_id}&service_city_id={$service_city_id}");
		}
		$province_id = $this->request->get('province_id', 0);
		$service_city_id = $this->request->get('service_city_id', 0);
		$casy_id = $this->request->get('casy_id', 0);
		if ($province_id<=0 || $service_city_id<=0 || $casy_id<=0) error('缺少参数');
		$rs = SQL::share('service_casy_detail')->where("casy_id='{$casy_id}'")->sort('id ASC')->find();
		$this->smarty->assign('rs',$rs);
		$city_name = SQL::share('service_city sc')
			->left('city c', 'c.city_id=sc.city_id')
			->where("sc.id='{$service_city_id}'")->val('c.name');
		$this->smarty->assign('city_name',$city_name);
		$this->display();
	}
}
